var express = require('express');
var Mock = require('mockjs');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
});


//--------------------------数据操作start-------------------------------
//浏览指定数据
router.post('/api/rest/v3/workspaces/:wsId/models/:mId/rows/filter/:filterId/', function(req, res) {
    var postData = req.body || {}; //请求来的对象参数
    var page = Number(postData['paging[currentPage]']);
    var rowCount = Number(postData['paging[rowCount]']);
    var colSortPredicate = postData['paging[colSort][predicate]'];
    var colSortWay = postData['paging[colSort][sort-way]'];
    console.log("postData=");
    console.log(postData);
    console.log("page=" + page);
    console.log("rowCount=" + rowCount);
    console.log("colSortPredicate=" + colSortPredicate);
    console.log("colSortWay=" + colSortWay);

    var maxCount = 150;
    var rowCountRegxp = "body|" + 10;
    if (page * rowCount - maxCount <= 0) {
        rowCountRegxp = "body|" + rowCount;
    } else {
        rowCountRegxp = "body|" + Number(page * rowCount - maxCount);
    }

    var mockRegxp = {
        "data": {
            "body": [{
                "c_10001": "@string('lower',1,10)",
                "c_10002": "@integer(0,1000)",
                "c_10003": "@float(0,100,2,2)",
                "c_10004": "@now",
                "c_10005|1": ["item1","item2","item3","item4","item5"],
                "rowId|+1": 1024 + (page - 1) * rowCount,
            }],
            "head|5": [{
                "column-name|+1": ["c_10001", "c_10002", "c_10003", "c_10004", "c_10005"],
                "dataType": {
                    "id|+1": [1, 2, 3, 4, 5],
                    "name|+1": ["string", "int", "float", "date", "string"]
                },
                "format": {
                    "id": 1,
                    "name": "hehehe"
                },
                "id|+1": 10001,
                "name|+1": ["模型字段1(String)", "模型字段2(int)", "模型字段(float)", "模型字段(date)", "模型字段(multiSelect)"],
                "type": {
                    "id": 1,
                    "name": "hehe"
                }
            }]
        },
        "message": "'Query Successfully'",
        "paging": {
            "colSort": {
                "predicate": colSortPredicate,
                "sort-way": colSortWay
            },
            "currentPage": page,
            "rowCount": rowCount,
            "rowTotal": maxCount
        },
        "result": 0
    };

    //替换body
    var oldVal = mockRegxp["data"]["body"];
    var newKey = rowCountRegxp;
    delete mockRegxp["data"]["body"];
    mockRegxp["data"][newKey] = oldVal;

    var data = Mock.mock(mockRegxp);
    setTimeout(function(){
        res.json(data);  
    },2000);
});

//修改数据，添加数据后保存数据的接口
router.put('/api/rest/v3/workspaces/:wsId/models/:mId/rows/:rowId', function(req, res) {
    var postData = req.body || {}; //请求来的对象参数
    console.log(postData);

    var data = Mock.mock({
        "data": {},
        "message": "'Query Successfully'",
        "result": 0
    });

    res.json(data);
});

//删除单条数据
router.delete('/api/rest/v3/workspaces/:wsId/models/:mId/rows/:rowId', function(req, res) {
    var postData = req.body || {}; //请求来的对象参数
    console.log(postData);

    var data = Mock.mock({
        "data": {},
        "message": "'Query Successfully'",
        "result": 0
    });

    res.json(data);
});

//批量删除数据
router.delete('/api/rest/v3/workspaces/:wsId/models/:mId/rows/', function(req, res) {
    var postData = req.body || {}; //请求来的对象参数
    console.log(postData);

    var data = Mock.mock({
        "data": {},
        "message": "'Query Successfully'",
        "result": 0
    });

    res.json(data);
});

//查询单条数据
router.get('/api/rest/v3/workspaces/:wsId/models/:mId/rows/:rowId', function(req, res) {
    var postData = req.body || {}; //请求来的对象参数
    console.log(postData);

    var data = Mock.mock({
        "Data": {
            "c_10001": {
                "errorMsg": "",
                "extraData": "",
                "format": "",
                "pattern": "",
                "required": true,
                "type": "text",
                "value|1": "@string"
            },
            "c_10002": {
                "errorMsg": "只能输入整数",
                "extraData": "",
                "format": "",
                "pattern": "[0-9]*",
                "required": true,
                "type": "number",
                "value": "@integer(0,1000)"
            },
            "c_10003": {
                "errorMsg": "只支持两位小数",
                "extraData": "",
                "format": "",
                "pattern": "[0-9]*[.][0-9]{0,2}",
                "required": true,
                "type": "number",
                "value": "@float(0,100,1,2)"
            },
            "c_10004": {
                "errorMsg": "",
                "extraData":"" ,
                "format": "yyyy-mm-dd",
                "pattern": "",
                "required": true,
                "type": "date",
                "value": "2015-3-22"
            },
            "c_10005": {
                "errorMsg": "",
                "extraData|4": [{
                    "content|+1": ["item1","item2","item3","item4"],
                    "value|+1": ["item1","item2","item3","item4"]
                }],
                "format": "",
                "pattern": "",
                "required": true,
                "type": "select",
                "value": "item1"
            }
        },
        "Meta|5": [{
            "column-name|+1": ["c_10001", "c_10002", "c_10003", "c_10004", "c_10005"],
            "name|+1": ["模型字段1(String)", "模型字段2(int)", "模型字段(float)", "模型字段(date)", "模型字段(multiSelect)"]
        }],
        "message": "'Query Successfully'",
        "result": 0,
        "rowId": "@DATETIME(HHmmss)"
    });

    res.json(data);
});

//--------------------------数据操作end-------------------------------

//--------------------------模型操作end-------------------------------
//查询所有模型
router.post('/api/rest/v3/workspaces/:wsId/models/rows/filter/:filterId/', function(req, res) {
    var postData = req.body || {}; //请求来的对象参数
    var page = Number(postData['paging[currentPage]']);
    var rowCount = Number(postData['paging[rowCount]']);
    var colSortPredicate = postData['paging[colSort][predicate]'];
    var colSortWay = postData['paging[colSort][sort-way]'];
    console.log("postData=");
    console.log(postData);
    console.log("page=" + page);
    console.log("rowCount=" + rowCount);
    console.log("colSortPredicate=" + colSortPredicate);
    console.log("colSortWay=" + colSortWay);

    var maxCount = 150;
    var rowCountRegxp = "body|" + 10;

    if (page * rowCount - maxCount <= 0) {
        rowCountRegxp = "body|" + rowCount;
    } else {
        rowCountRegxp = "body|" + Number(page * rowCount - maxCount);
    }

    var modelNameCount = 1; //用来作为命名modelN中最后的N
    var mockRegxp = {
        "data": {
            "body": [{
                "rowId|+1": 1024 + (page - 1) * rowCount,
                "c_10001": function() {
                    return "model" + ((modelNameCount++) + (page - 1) * rowCount);
                },
                "c_10002": "@now",
                "c_10003": "@cname",
                "createTime": "@now",
                "createUser": function() {
                    var random = Mock.Random;
                    var id = random.integer(1000, 2000);
                    var name = random.cname();
                    return {
                        "id": id,
                        "name": name,
                        "showName": "Dept" + id + "-" + name
                    }
                },
                "description": "测试内容83w1",
                "groups": "分组1,分组2,分组3",
                "id|+1": 1001,
                "name": function() {
                    return "model" + (modelNameCount - 1);
                },
                "status": function() {
                    var random = Mock.Random;
                    var id = random.integer(0, 2);
                    var name = "启用";
                    switch (id) {
                        case 0:
                            name = "未启用";
                            break;
                        case 1:
                            name = "启用";
                            break;
                        case 2:
                            name = "停用";
                            break;
                        default:
                            name = "停用";
                    }
                    return {
                        "id": id,
                        "name": name
                    }
                },
                "updateTime": "@now",
                "updateUser": function() {
                    var random = Mock.Random;
                    var id = random.integer(1000, 2000);
                    var name = random.cname();
                    return {
                        "id": id,
                        "name": name,
                        "showName": "Dept" + id + "-" + name
                    }
                }
            }],
            "head|3": [{
                "column-name|+1": ["c_10001", "c_10002", "c_10003"],
                "dataType": {
                    "id|+1": [1, 2, 3],
                    "name|+1": ["string", "date", "string"]
                },
                "format": {
                    "id": 1,
                    "name": "hehehe"
                },
                "id|+1": 10001,
                "name|+1": ["模型名称", "更新时间", "创建人"],
                "type": {
                    "id": 1,
                    "name": "hehe"
                }
            }]
        },
        "message": "'Query Successfully'",
        "paging": {
            "colSort": {
                "predicate": colSortPredicate,
                "sort-way": colSortWay
            },
            "currentPage": page,
            "rowCount": rowCount,
            "rowTotal": maxCount
        },
        "result": 0
    };

    //替换body
    var oldVal = mockRegxp["data"]["body"];
    var newKey = rowCountRegxp;
    delete mockRegxp["data"]["body"];
    mockRegxp["data"][newKey] = oldVal;

    var data = Mock.mock(mockRegxp);

    setTimeout(function(){
        res.json(data);  
    },2000);
});

module.exports = router;
